/**
 * ExtraCommon
 */

  import imageHelper from "../../../util/ImageHelper";

  var tool = require("../../../util/tool");
  var consts = require("../../../util/consts");
  var ExtraCommon = {
      componentWillMount: function (){
          var widget = this.props.widget;

          this.shouldUpdate = false;
          this.custom = widget.custom * 1;
          this.isRedbag = (widget.type === consts.WIDGET_TYPE.REDBAG);
          this.isHopeBank = (widget.type === consts.WIDGET_TYPE.HOPEBANK);

          this.src = (this.isRedbag && (!this.custom || !widget.icon_src && !widget.url)) ?
            consts.REDBAG_DEFAULT_SRC : (this.isHopeBank && (!this.custom || !widget.icon_src && !widget.url)) ?
              consts.HOPEBANK_DEFAULT_SRC : (widget.icon_src || widget.url);
      },
      componentDidMount: function (){
          if (this.custom || this.isRedbag || this.isHopeBank){
              imageHelper.preload(this.refs.image, this.src);
          }
      },
      componentWillReceiveProps: function (nextProps){
          var current = this.widget, next = nextProps.widget;

          var currentCustom = current.custom * 1;
          var currentSrc = (this.isRedbag && (!currentCustom || !current.icon_src && !current.url)) ?
              consts.REDBAG_DEFAULT_SRC : (this.isHopeBank && (!currentCustom || !current.icon_src && !current.url)) ?
              consts.HOPEBANK_DEFAULT_SRC : (current.icon_src || current.url);

          this.custom = next.custom * 1;
          // 红包或者希望金币默认显示相应默认图片
          this.src = (this.isRedbag && (!this.custom || !next.icon_src && !next.url)) ?
              consts.REDBAG_DEFAULT_SRC : (this.isHopeBank && (!this.custom || !next.icon_src && !next.url)) ?
                consts.HOPEBANK_DEFAULT_SRC : (next.icon_src || next.url);

          // 判断是否强制更新
          this.shouldUpdate = ((currentCustom !== this.custom) || (currentSrc !== this.src));
      },
      componentDidUpdate: function (nextProps, nextState){
          if (this.shouldUpdate){
              imageHelper.preload(this.refs.image, this.src);
          }
      }
  };

  module.exports = ExtraCommon;
